import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:latlong2/latlong.dart';
import '../../pages/gmap/gmap3.dart';

import '../../helper/routeHelper.dart';
import '../../model/scqx_r.dart';
import '../../pages/sdzx/sdzx_helper.dart';
import '../../widgets/inputWidget.dart';
import '../gmap/gmap2.dart';
import '../gmap/gmapHelper.dart';

import '../../model/station.dart';

class SdzxMapPage extends StatefulWidget {
  @override
  SdzxMapPageState createState() => new SdzxMapPageState();
}

class SdzxMapPageState extends State<SdzxMapPage> {
  List<Station> StationList = <Station>[];
  List<ScqxR> ScqxRList = <ScqxR>[];
  String RType = "24小时累计值";
  List<String> RTList = SdzxHelper.RainList();
  List<LayerOptions> layer = <LayerOptions>[];

  @override
  initState() {
    super.initState();

    loadData();
  }

  void onRTypeChange(dynamic nn) {
    String yy = nn.toString();
    RType = yy;

    setState(() {
      RType = yy;
    });
    print(RType);
    loadData();
  }

  void loadData() async {
    // var _list = await CacheHelper.GetStationList();
    var list = await ScqxR.GetAllList();

    setState(() {
      // StationList = _list;
      ScqxRList = list;
    });
  }

  Widget getSdzxMap() {
    if (ScqxRList.isEmpty) return Container();

    layer = this.getScqxLayer(ScqxRList);

    return Stack(
      alignment: Alignment.center,
      fit: StackFit.expand, //未定位widget占满Stack整个空间
      children: <Widget>[
        MyMap3(layers: layer),
        Positioned(
          top: 18.0,
          child: InputWidget.getSelect2(RTList, RType,
              onChanged: this.onRTypeChange, col: Colors.blue),
        ),
        Positioned(
          bottom: 12.0,
          width: 360,
          child: Container(child: getIconPanel()),
        ),
      ],
    );
    // return MyMap(layers: layer);
  }

  Widget getIconPanel() {
    var v1 = getIconSM(0.1, "<1");
    var v2 = getIconSM(2, "1-5");
    var v3 = getIconSM(7, "5-15");
    var v4 = getIconSM(20, "15-30");
    var v5 = getIconSM(40, "30-70");
    var v6 = getIconSM(80, "70-140");
    var v7 = getIconSM(180, ">140");

    return InputWidget.getShowRow([v1, v2, v3, v4, v5, v6, v7], false);
  }

  Widget getIconSM(double v1, String s1) {
    var x1 = getIconCircle(getRainColor(v1));
    return Row(children: [
      x1,
      Text(
        s1,
        textScaleFactor: 1.0,
        style: TextStyle(fontSize: 11.0),
      )
    ]);
  }

  Widget getIconCircle(Color col1) {
    return Container(
        margin: EdgeInsets.only(right: 2.0),
        width: 10.0,
        height: 10.0,
        child: new CircleAvatar(
          backgroundColor: col1,
          //radius: 2.0,
        ));
  }

  List<LayerOptions> getScqxLayer(List<ScqxR> pps) {
    var xList = <LayerOptions>[];
    xList.add(GMapHelper.TileShuiXiMap());
    xList.addAll(GMapHelper.DituLayerList());
    for (int j = 0; j < pps.length; j++) {
      var p = new LatLng(pps[j].lat, pps[j].lng);

      xList.add(GMapHelper.BJLayer(p, 60, 80, getScqxIcon(pps[j])));
    }

    return xList;
  }

  Widget getScqxIcon(ScqxR x1) {
    double v1 = SdzxHelper.getRainVal(x1, RType);
    //  print([v1,x1.pr24]);
    var col1 = getRainColor(v1);
    Widget v2;

    if (v1 > 0) {
      v2 = Text(v1.toStringAsFixed(1), style: TextStyle(color: col1));
    } else {
      v2 = Container();
    }

    var v4 = getIconCircle(col1);
    //onTap: ()=>ChannelHelper.showToast(x1.sname),

    return GestureDetector(
      child: Center(child: Row(children: [v4, v2])),
      onTap: () =>
          RouteHelper.showBottomPanel(context, SdzxHelper.getRainPanel(x1)),
    );
  }

  Color getRainColor(double v1) {
    if (v1 < 1) return Colors.grey;
    if (v1 < 5) return Colors.green;
    if (v1 < 15) return Colors.lightBlue;
    if (v1 < 30) return Colors.blue;
    if (v1 < 70) return Colors.orange;
    if (v1 < 140) return Colors.pinkAccent;
    if (v1 >= 140) return Colors.purple;
    return Colors.grey;
  }

  Widget getBody() {
    if (ScqxRList == null)
      return Center(
          child: SpinKitCircle(
        color: Colors.blue,
      ));
    // TODO: implement build
    return Center(
        // margin: EdgeInsets.only(top: 8.0),
        child: getSdzxMap());
  }

  @override
  Widget build(BuildContext context) {
    //getStation();
    return getBody();
  }
}
